using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using AopLoadBalancer.Server;
using Protocols_SoapHeader = System.Web.Services.Protocols.SoapHeader;
using AopLoadBalancer.Server.BusinessLogic;

namespace AopLoadBalancer.Sample.Server
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service : System.Web.Services.WebService
    {
        /*
        public static void CheckIsFirst()
        {
            if (Global.AliveNotifier == null)
            {
                Global.isFirst = false;
                Global.aliveNotifier = new AliveNotifier(Global.ServiceUrl, Global.ServiceId);
                Global.aliveHostTracker = new AliveHostTracker(Global.ServiceId, Global.ServiceUrl);

                Global.aliveNotifier.Enabled = true;
            }
        }
        */
        public static void CalculatePi()
        {
            bool silent = false;

            uint i = 0;
            double num, den;
            double step, qpi;

            qpi = 0.0;

            while (i < 300)
            {
                num = -1;
                for (int j = 0; j <= i; j++)
                    num *= -1;

                den = 2 * i + 1;

                step = num / den;
                qpi += step;

                i++;
            }
        }

        private AvailableHostsHeader availableHosts;


        public AvailableHostsHeader AvailableHosts
        {
            get { return availableHosts; }
            set { availableHosts = value; }
        }

        public Service()
        {

        }

        [WebMethod]
        [SoapHeader("AvailableHosts", Direction = SoapHeaderDirection.Out)]
        public string SimpleCall(string parameter)
        {
            if (!Global.AliveNotifier.Enabled)
            {
                throw new Exception();
            }

            availableHosts = new AvailableHostsHeader();
            availableHosts.Hosts = Global.AliveHostTracker.GetAliveHosts().ToArray();

            return parameter;
        }

        [WebMethod]
        [SoapHeader("AvailableHosts", Direction = SoapHeaderDirection.Out)]
        public ResponseMessage ComplexCall(RequestMessage request)
        {
            //CheckIsFirst();

            if (!Global.AliveNotifier.Enabled)
            {
                throw new Exception();
            }

            CalculatePi();

            availableHosts = new AvailableHostsHeader();
            availableHosts.Hosts = Global.AliveHostTracker.GetAliveHosts().ToArray();


            return new ResponseMessage();
        }

        [WebMethod()]
        public void Enable()
        {
            //CheckIsFirst();

            Global.AliveNotifier.Enabled = true;
        }

        [WebMethod()]
        public void Disable()
        {
            Global.AliveNotifier.Enabled = false;
        }

        [WebMethod()]
        public bool State()
        {
            return Global.AliveNotifier.Enabled;
        }
    }
}
